﻿using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Share;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.ACTP.Components;
using ASPNETVN.PORTAL.Components.Security;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class ProjectDetail : ModulePage
    {
        protected int TeamID;
        private void BindTasks()
        {
            int projectID = this.ItemID;
            //var projecTasks = ProjectTask.SelectCollectionBy_ProjectID(projectID);
            //using (ACTPDataContext context = new ACTPDataContext())
            //{
            //    var tasks = from T in context.t_ACTP_Tasks
            //                join PT in context.t_ACTP_ProjectTasks on T.TaskID equals PT.TaskID
            //                where PT.ProjectID == projectID
            //                select T;
            //}
            
            //projecTasks
            //rptTaskMilestones.DataSource = TaskMilestone.SelectCollectionBy_ProjectID(this.ItemID);
            //rptTaskMilestones.DataBind();
            //foreach (RepeaterItem item in rptTaskMilestones.Items)
            //{
            //    GridView gv = (GridView)item.FindControl("gvTasks");
            //    if (gv != null)
            //    {
            //        gv.DataSource = Task.SelectBy_ProjectID_Include_Employee(this.ModuleID, projectID).Tables[0].Select("");
            //    }
            //}

            if (PortalSecurity.HasManageAuthorization(this.ModuleID) || PortalSecurity.IsInRoles("1"))
            {
                gvTasks.DataSource = Task.SelectBy_ProjectID_Include_Employee(this.ModuleID, projectID);
                gvTasks.DataBind();
            }
            else
            {
                int employeeID = Employee.GetEmployeeBy_MembershipID(PortalSecurity.CurrentUser.Identity.ID).EmployeeID;
                gvTasks.DataSource = Task.SelectBy_ProjectID_EmployeeID_Include_Employee(this.ModuleID, projectID, employeeID);
                gvTasks.DataBind();
            }
        }

        protected string GetProjectManagerAsHtml(object o)
        {
            int projectID = Convert.ToInt32(o);
            Employee employee = Employee.GetEmployeeBy_ProjectID_ProjectManager(this.ModuleID, projectID);
            if (employee != null)
                return employee.EmployeeCode;
            return string.Empty;
        }

        private void BindEmployees()
        {
            IList<Employee> source = Employee.SelectCollectionBy_ProjectID(this.ModuleID, this.ItemID);
            gvEmployees.DataSource = source;
            gvEmployees.DataBind();
        }

        private void BindTaskMilestones()
        {

            IList<TaskMilestone> source = TaskMilestone.SelectCollectionBy_ProjectID(this.ItemID);
            gvTaskMilestone.DataSource = source;
            gvTaskMilestone.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // BEGIN: Project details

                if (this.ItemID > 0)
                {
                    ProjectDetailControl1.ProjectID = this.ItemID;
                    Project project = Project.Load(this.ItemID);
                    this.TeamID = project.TeamID;

                    // Tasks.
                    this.BindTasks();

                    // Employees.
                    this.BindEmployees();

                    // TaskMilestones.
                    this.BindTaskMilestones();
                }
                // END: Project detail
            }
        }

        protected void btnAddTask_Click(object sender, EventArgs e)
        {
            Project project = Project.Load(this.ItemID);
            if (project != null)
            {
                string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditTask", "teamid={0}&projectid={1}", project.TeamID, project.ProjectID);
                Response.Redirect(url);
            }
        }

        protected void DataListAttachment_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int ret = Attachment.DeleteAttachment(Convert.ToInt32(DataListAttachment.DataKeys[e.Item.ItemIndex]));
            if (ret > 0)
            {
                if (this.Settings["StorageType"].ToString().ToUpper().Equals("FILE"))
                {
                    string filename = this.Server.MapPath("~/Uploads/" + this.ModuleID + "/" + e.CommandArgument);
                    if (File.Exists(filename))
                    {
                        File.Delete(filename);
                    }
                }
                DataListAttachment.DataBind();
            }
        }

        protected void btnAddEmployee_Click(object sender, EventArgs e)
        {
            Project project = Project.Load(this.ItemID);
            if (project != null)
            {
                string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEmployeeToProject", "teamid={0}&projectid={1}", project.TeamID, project.ProjectID);
                Response.Redirect(url);
            }
        }


        protected void btnAddDocument_Click(object sender, EventArgs e)
        {
            // Go to upload file page.
            string returnUrl = HttpUtility.UrlEncode(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.ProjectDetail));
            string url = HttpUrlBuilder.BuildShareUrl(this.ModuleID, this.ItemID, "UploadFile", "returnurl={0}", returnUrl);
            Response.Redirect(url);
        }

        protected void btnAddEmployeeToProject_Click(object sender, EventArgs e)
        {
            Project project = Project.Load(this.ItemID);
            if (project != null)
            {
                string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEmployeeToProject", "teamid={0}&projectid={1}", project.TeamID, project.ProjectID);
                Response.Redirect(url);
            }
        }

        protected void btnAddTaskMilestone_Click(object sender, EventArgs e)
        {
            Project project = Project.Load(this.ItemID);
            if (project != null)
            {
                string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditTaskMilestone", "teamid={0}&projectid={1}", project.TeamID, project.ProjectID);
                Response.Redirect(url);
            }
        }

        protected void btnViewGanttChart_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.ProjectGanttChart);
            Response.Redirect(url);
        }

        protected void btnViewProjectCalendar_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.ProjectCalendar);
            Response.Redirect(url);
        }
    }
}